<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/common.jsp"%>
<%
	//查询前十名的诊断名
	String yydm =  input.getDesc("yydm","01");//医院代码	
	String ksdm = input.getDesc("ksdm","-1"); //传入科室代码代表该科室主任
	String date = input.getDesc("start_date","2017-12-26");//开始时间
	String endDa = input.getDesc("end_date","2017-12-26");//结束时间

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
	String nowDate = sdf2.format(sdf.parse(date));
	String nowEndDa = sdf2.format(sdf.parse(endDa));
	
	String startDate = nowDate+"00:00:00";
	String endDate = nowEndDa+"23:59:59";

	String sqlWhere = " and zy.ryrq > '"+ startDate + "' and zy.ryrq < '" + endDate+"' " ;
	if(!"-1".equals(ksdm)){
		sqlWhere += " and zy.ksdm in( " + ksdm +")";
	
	}
	if(!yydm.equals("-1"))
	sqlWhere += " and zy.ksdm in (select id  from his_dept where yydm ='"+yydm+"') " ;
	//获取所有的诊断
	String dHql = "select new map(zdmc as zdmc , zddm as zddm) from mzzd ";
	List<Map> zddList =QBuilder.db("basedb").queryHql(dHql);
	Map zdMap = new HashMap();
	for(Map map : zddList){
		zdMap.put(map.get("zddm"),map.get("zdmc"));
	}
	
	//前十名诊断
	
	String hql = "select new map(zy.zddm as zddm,count(zy.zddm) as count) from ZY_BRSYK zy   where 1=1 and zy.zddm is not null and zy.zddm != '' " + sqlWhere + " group by zy.zddm order by count(zy.zddm) desc";
	
	List<Map> zdList =QBuilder.db("basedb").queryHql(hql);
	List<Map> resList = new ArrayList();
	for(Map map : zdList){
		if(zdMap.get(map.get("zddm")) == null){
			continue;
		}
		map.put("zdmc",zdMap.get(map.get("zddm")) );
		resList.add(map);
		if(resList.size() >= 10){
			break;
		}
	}
	for(Map map : zdList){
		map.put("count",map.get("count")+"");
	}
	Message msg = new Message();
	msg.put("errcode",0);
	msg.put("diagnosed",resList);
	msg.put("count",resList.size());
	out.println(msg);
%>